package com.youlai.boot.system.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.common.annotation.RepeatSubmit;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.common.exception.BusinessException;
import com.youlai.boot.system.model.vo.LpsOrderImportExcelResultVO;
import com.youlai.boot.system.service.LpsOrderService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.LpsOrderForm;
import com.youlai.boot.system.model.query.LpsOrderQuery;
import com.youlai.boot.system.model.vo.LpsOrderVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 莱普森订单前端控制层
 *
 * @author lwb
 * @since 2025-07-13 16:00
 */
@Tag(name = "莱普森订单接口")
@RestController
@RequestMapping("/api/v1/lpsOrders")
@RequiredArgsConstructor
public class LpsOrderController  {

    private final LpsOrderService lpsOrderService;

    @Operation(summary = "莱普森订单分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:lpsOrder:query')")
    public PageResult<LpsOrderVO> getLpsOrderPage(LpsOrderQuery queryParams ) {
        IPage<LpsOrderVO> result = lpsOrderService.getLpsOrderPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增莱普森订单")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:lpsOrder:import')")
    //@Log(value = "新增订单", module = LogModuleEnum.OTHER)
    @RepeatSubmit(expire = 3)
    public Result<Void> saveLpsOrder(@RequestBody @Valid LpsOrderForm formData ) {
        boolean result = lpsOrderService.saveLpsOrder(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取莱普森订单表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:lpsOrder:edit')")
    public Result<LpsOrderForm> getLpsOrderForm(
        @Parameter(description = "莱普森订单ID") @PathVariable Long id
    ) {
        LpsOrderForm formData = lpsOrderService.getLpsOrderFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改莱普森订单")
    @PutMapping(value = "/{id}")
    @RepeatSubmit(expire = 3)
    @PreAuthorize("@ss.hasPerm('system:lpsOrder:edit')")
    public Result<Void> updateLpsOrder(
            @Parameter(description = "莱普森订单ID") @PathVariable Long id,
            @RequestBody @Validated LpsOrderForm formData
    ) {
        boolean result = lpsOrderService.updateLpsOrder(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除莱普森订单")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:lpsOrder:delete')")
    @Log(value = "删除订单", module = LogModuleEnum.OTHER)
    public Result<Void> deleteLpsOrders(@Parameter(description = "莱普森订单ID，多个以英文逗号(,)分割") @PathVariable String ids){
        boolean result = lpsOrderService.deleteLpsOrders(ids);
        return Result.judge(result);
    }

    @Operation(summary = "导入")
    @PostMapping("/import")
    @RepeatSubmit(expire = 3)
    @PreAuthorize("@ss.hasPerm('system:lpsOrder:import')")
    public Result<LpsOrderImportExcelResultVO> importExcel(@RequestParam("files")List<MultipartFile>  files) throws Exception {
        if (CollectionUtil.isEmpty(files)) {
            throw new BusinessException("文件为空，请上传有效的Excel文件");
        }
        return Result.success(lpsOrderService.importExcel(files));
    }
    @Operation(summary = "下载订单")
    @GetMapping("/download")
    @PreAuthorize("@ss.hasPerm('system:lpsOrder:download')")
    @RepeatSubmit(expire = 3)
    public void download(LpsOrderQuery queryParams,HttpServletResponse response) throws Exception {
        lpsOrderService.download(queryParams, response);
    }
}
